package com.fsc.app_news;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener{
	  private LinearLayout ll_home;//主页
	  private LinearLayout ll_video;//视频
	  private LinearLayout ll_other;//其它
	  private Fragment fl_content;
	  private FragmentManager manager;//管理
	  
	  private Fragment HomeActivity;
	  private Fragment OtherActivity;
	  private Fragment VideoActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		envet();
		setSelect(1);
		
	}
	
	public void init(){
		ll_home = (LinearLayout) findViewById(R.id.ll_home);
		ll_video = (LinearLayout) findViewById(R.id.ll_video);
		ll_other = (LinearLayout) findViewById(R.id.ll_other);
	}
	public void envet() {
		ll_home.setOnClickListener(this);
		ll_video.setOnClickListener(this);
		ll_other.setOnClickListener(this);
	}
	//初始化颜色
	private void resetColor() {
		ll_home.setBackgroundColor(Color.parseColor("#232425"));
		ll_video.setBackgroundColor(Color.parseColor("#232425"));
		ll_other.setBackgroundColor(Color.parseColor("#232425"));
	}
	//设置选中按钮后执行的操作
	private void setSelect(int index){
		manager = getFragmentManager();
		FragmentTransaction ft = manager.beginTransaction();
		//首先隐藏所有界面
		hiden(ft);
		//点击按钮之后改变按钮颜色,并且切换页面
		switch (index) {
		case 1:
			if(HomeActivity == null){
				HomeActivity = new HomeActivity();
				ft.add(R.id.fl_content, HomeActivity);
			}else{
				ft.show(HomeActivity);
			}
			ll_home.setBackgroundColor(Color.parseColor("#E7E7E7"));
			break;
		case 2:
			if(VideoActivity == null){
				VideoActivity = new VideoActivity();
				ft.add(R.id.fl_content, VideoActivity);
			}else{
				ft.show(VideoActivity);
			}
			ll_video.setBackgroundColor(Color.parseColor("#E7E7E7"));
			break;
		case 3:
			if(OtherActivity == null){
				OtherActivity = new OtherActivity();
				ft.add(R.id.fl_content, OtherActivity);
			}else{
				ft.show(OtherActivity);
			}
			ll_other.setBackgroundColor(Color.parseColor("#E7E7E7"));
			break;
		}
		ft.commit();
	}
	private void hiden(FragmentTransaction ft) {
		if(HomeActivity != null){
			ft.hide(HomeActivity);
		}
		if(VideoActivity != null){
			ft.hide(VideoActivity);
		}
		if(OtherActivity != null){
			ft.hide(OtherActivity);
		}
	}
	@Override
	public void onClick(View v) {
	resetColor();
	switch (v.getId()) {
		case R.id.ll_home:
			setSelect(1);
			break;
		case R.id.ll_video:
			setSelect(2);
			break;
		case R.id.ll_other:
			setSelect(3);
			break;
		}
	}
}
